package com.stlm2.dems.service;

import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.DdmObligationDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.DdmObligation;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.GoodsObligation;
import com.stlm2.dems.dao.GoodsObligationDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("goodsObligationService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class GoodsObligationService extends BaseServiceImpl<GoodsObligation> {

	@Resource
	private GoodsObligationDao goodsObligationDao;
	@Resource
	private UserDao userDao;
	@Resource
	private DdmGoodsDao ddmGoodsDao;
	@Resource
	private DdmObligationDao ddmObligationDao;
	@Resource
	private Memcached memcached;

	@Override
	protected BaseDao <GoodsObligation> getDao() {
		return goodsObligationDao;
	}
	/*
	*zlq
	* 商品添加叮当猫服务承诺
	* token:
	* goodsId:商品ID
	* ddmObligationIds：叮当猫服务ID
	 */
	public String add(ModelMap model, String token,String goodsId,String ddmObligationIds){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (StringUtils.isEmpty(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		if (StringUtils.isEmpty(ddmObligationIds)) {
			return CodeContent.getModelByCode(0, "服务承诺不能为空", model);
		}
		DdmGoods goods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		if(goods==null){
			return CodeContent.getModelByCode(0, "商品ID不存在", model);
		}
		//防止重复提交
		Object o = memcached.get(goods.getId()+"addGoodsObligation");
		if(o!=null){
			return CodeContent.getModelByCode(0, "操作频繁", model);
		}else {
			memcached.set(goods.getId()+"addGoodsObligation",1,"1");
		}
		List <GoodsObligation>  goodsObligationList=new ArrayList<GoodsObligation>();
		String [] ddmObligations=ddmObligationIds.split(",");
		for(String ddmObligationId: ddmObligations){
			DdmObligation ddmObligation =ddmObligationDao.getById(Integer.parseInt(ddmObligationId));
			if(ddmObligation==null){
				return CodeContent.getModelByCode(0, "服务承诺ID不存在", model);
			}
			GoodsObligation goodsObligation=new GoodsObligation();
			goodsObligation.setGoodsId(goods.getId());
			goodsObligation.setDdmObligationId(ddmObligation.getId());
			List<GoodsObligation>  goodsObligationList1=goodsObligationDao.queryPageView(-1,-1,goodsObligation).getRecords();
			if(goodsObligationList1.size()!=0){
				return CodeContent.getModelByCode(0, "商品已经添加过:" + ddmObligation.getName(), model);
			}
			goodsObligationList.add(goodsObligation);
		}
		for(GoodsObligation goodsObligation:goodsObligationList){
			goodsObligationDao.add(goodsObligation);
		}
		return CodeContent.getModelByCode(1, "", model);
	}

	/*
*商品删除服务承诺
* zlq
* token:
* id: 记录ID
 */
	public String delete(ModelMap model, String token,String id){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(id)) {
			return CodeContent.getModelByCode(0, "ID不能为空", model);
		}
		GoodsObligation goodsObligation=goodsObligationDao.getById(Integer.parseInt(id));
		if(goodsObligation==null){
			return CodeContent.getModelByCode(0, "ID不存在", model);
		}
		goodsObligationDao.delete(goodsObligation.getId());

		return CodeContent.getModelByCode(1, "", model);
	}

	/*
*zlq
* 商品查找叮当猫服务承诺列表
* token:
* goodsId:商品ID
 */
	public String getlist(ModelMap model, String token,String goodsId) {
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (StringUtils.isEmpty(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		DdmGoods ddmGoods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		if(ddmGoods!=null){
			List<GoodsObligation> goodsObligationList=goodsObligationDao.getByGoodsId(ddmGoods.getId());
			return CodeContent.getModelByCode(1, "", convertList(goodsObligationList));
		}

		return CodeContent.getModelByCode(1, "", model);
	}

	public List<ModelMap> convertList(List<GoodsObligation> goodsObligationList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsObligationList != null && goodsObligationList.size() > 0) {
			for (GoodsObligation goodsObligation : goodsObligationList) {
				ModelMap map = buildModelList(goodsObligation);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(GoodsObligation goodsObligation) {
		ModelMap model = new ModelMap();
		model.put("id",goodsObligation.getId());
		model.put("name",goodsObligation.getDdmObligationList().size()!=0?goodsObligation.getDdmObligationList().get(0).getName():"");
		return  model;
	}
}